import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { RoleApi } from '#/api/system/role';

import { useAccess } from '@vben/access';

import { z } from '#/adapter/form';
import { getMenuList } from '#/api/system/menu';

const { hasAccessByCodes } = useAccess();

/** 角色表单Schema */
export function useRoleFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'systemId',
      label: '所属系统',
      component: 'Select',
      componentProps: {
        placeholder: '请选择所属系统',
        allowClear: false,
        options: [],
      },
      rules: 'required',
    },
    {
      fieldName: 'title',
      label: '角色名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入角色名称',
      },
      rules: 'required',
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      componentProps: {
        options: [
          { label: '启用', value: 1 },
          { label: '禁用', value: 2 },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(1),
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注信息',
        rows: 3,
      },
    },
    {
      fieldName: 'menuIds',
      label: '菜单权限',
      component: 'Tree', // 使用注册的Tree组件
      dependencies: {
        triggerFields: ['systemId'],
        show: (values) => !!values.systemId && values.systemId !== undefined,
      },
      componentProps: (values) => ({
        api: getMenuList,
        params: values.systemId ? { systemId: values.systemId } : {},
        defaultProps: { label: 'title' },
        nodeKey: 'id',
        showCheckbox: true,
        multiple: true,
        expandOnClickNode: false,
        checkedKeys: values.menuIds,
        idField: 'id',
        pidField: 'pid',
        requiredParams: ['systemId'],
      }),
      formItemClass: 'items-start',
    },
  ];
}

/** 列表搜索表单Schema */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'systemId',
      label: '所属系统',
      component: 'Select',
      componentProps: {
        placeholder: '请选择系统',
        allowClear: true,
        options: [],
      },
    },
    {
      fieldName: 'title',
      label: '角色名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入角色名称',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        placeholder: '请选择状态',
        allowClear: true,
        options: [
          { label: '启用', value: 1 },
          { label: '禁用', value: 2 },
        ],
      },
    },
  ];
}

/** 列表表格列定义 */
export function useGridColumns<T = RoleApi.Role>(
  onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
  return [
    {
      type: 'seq',
      title: '序号',
      width: 50,
    },
    {
      field: 'systemName',
      title: '所属系统',
      minWidth: 150,
    },
    {
      field: 'title',
      title: '角色名称',
      minWidth: 150,
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 80,
      cellRender: {
        name: 'CellTag',
        props: {
          options: [
            { label: '启用', value: 1, color: 'success' },
            { label: '禁用', value: 2, color: 'danger' },
          ],
        },
      },
    },
    {
      field: 'remark',
      title: '备注',
      minWidth: 200,
    },
    {
      field: 'editInfo',
      title: '编辑信息',
      slots: { default: 'edit-info' },
      minWidth: 200,
    },
    {
      field: 'operation',
      title: '操作',
      width: 200,
      fixed: 'right',
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'title',
          nameTitle: '角色',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'edit',
            text: '编辑',
            show: hasAccessByCodes(['system:role:edit']),
          },
          {
            code: 'delete',
            text: '删除',
            show: hasAccessByCodes(['system:role:delete']),
          },
        ],
      },
    },
  ];
}
